package com.echat.business.music.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.echat.business.music.model.Music;
import com.echat.business.music.service.IMusicService;
import com.echat.config.common.RequestHolder;
import com.echat.core.constant.ConstantUtils;
import com.echat.core.constant.response.ErrorResponseData;
import com.echat.core.constant.response.ResponseData;
import com.echat.core.utils.FileUtil;
import com.echat.core.utils.Pair;
import com.echat.core.utils.ValueUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chentl123
 * @since 2019-04-19
 */
@Slf4j
@RestController
@RequestMapping(value = "/music")
public class MusicController {

    @Value("${echo-music.defaultCover}")
    private String defaultCover;

    @Autowired
    private IMusicService musicService;

    @RequestMapping(value = "/getMusicList", method = RequestMethod.POST)
    public ResponseData getMusicList(Music music) {
        System.out.println("current Thread" + Thread.currentThread());
        if (music == null) {
            return new ErrorResponseData("error");
        }
        Wrapper<Music> wrapper = new EntityWrapper<>();
        wrapper.eq("createby", music.getCreateby());
        List<Music> musicList = musicService.selectList(wrapper);
        return ResponseData.success(musicList);
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public ResponseData uploadMusic(@RequestParam(value = "file") MultipartFile[] fileArr) {
        for (MultipartFile file : fileArr) {
            Pair<String, String> filePair = FileUtil.uploadFile(file, "music");
            if (filePair == null) {
                return ResponseData.error("文件信息为空");
            }
            String fileName = null;
            fileName = filePair.first;
            String musicName = "";
            String artistName = "";
            String theme = "#" + ValueUtils.getRandColorCode();
            if (fileName == null) {
                return ResponseData.error("文件名为空");
            }
            if (fileName.contains(ConstantUtils.Public.MINUS)) {
                //视为网易云音乐的导出格式
                musicName = ValueUtils.formatterFileName(fileName.split(ConstantUtils.Public.MINUS)[1].trim());
                artistName = ValueUtils.formatterFileName(fileName.split(ConstantUtils.Public.MINUS)[0].trim());
            } else {
                musicName = fileName;
                artistName = "神秘人";
            }
            String url = filePair.second;
            //入库
            Music music = new Music(musicName, artistName, url, defaultCover, "", theme, RequestHolder.getCurrentUser().getId());
            musicService.insert(music);
        }
        return ResponseData.success();
    }
}

